var link = location.href;
var ctxPath = "/education";
var appId;
!function ($) {

    $.getUrlParam = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    };

    var initJsSdk = function() {
        $.ajax({
            data: {url: link},
            type: "GET",
            url: ctxPath + "/wx/api/signature",
            success: function (json) {
                if (json) {
                    var data = JSON.parse(json);
                    appId = data.appid;
                    wx.config({
                        debug: false,
                        appId: data.appid,
                        timestamp: data.timestamp,
                        nonceStr: data.nonceStr,
                        signature: data.signature,
                        jsApiList: [
                            'onMenuShareTimeline',
                            'onMenuShareAppMessage',
                            'onMenuShareQQ',
                            'onMenuShareWeibo',
                            'onMenuShareQZone',
                            'chooseWXPay'
                        ]
                    });
                    wx.ready(function () {
                        var title, imgUrl, description;
                        console.log("微信JS SDK配置成功！");
                        //实例化title\imgUrl
                        title = $('h1#productName').html()==null?"罗斯游学留学":$('h1#productName').html();  //默认取title
                        description = "罗斯游学留学";
                        //判断当前页面类型取缩略图
                        imgUrl = null;
                        $("body img").each(function (index, element) {
                                var width = $(element).width();
                                var height = $(element).height();
                                var Src = $(element).attr("src");
                                if (width > 200 && height > 200) {//去页面中第一张宽高都大于200的图片
                                    imgUrl = "http://" + location.host + Src;
                                    return false;
                                }
                            }
                        );
                        //分享到朋友圈
                        wx.onMenuShareTimeline({
                            title: title,
                            link: link,
                            imgUrl: imgUrl,
                            success: function () {
                                console.log("分享成功：" + title + ","+ link + "," + imgUrl)
                                //分享统计，分享来源 1 朋友圈 2分享给朋友  3分享到QQ  4分享到QQ空间

                            }
                        });

                        //分享给朋友
                        wx.onMenuShareAppMessage({
                            title: title,
                            desc: description,
                            link: link,
                            imgUrl: imgUrl,
                            success: function () {
                                console.log("分享成功：" + title + ","+ link + "," + imgUrl)
                                //分享统计，分享来源 1 朋友圈 2分享给朋友  3分享到QQ  4分享到QQ空间

                            }
                        });
                        //分享到
                        wx.onMenuShareQQ({
                            title: title,
                            desc: description,
                            link: link,
                            imgUrl: imgUrl,
                            success: function () {
                                console.log("分享成功：" + title + ","+ link + "," + imgUrl)
                                //分享统计，分享来源 1 朋友圈 2分享给朋友  3分享到QQ  4分享到QQ空间

                            }
                        });
                    });
                }
            }
        });
    };

    initJsSdk();
}(jQuery);